package com.qingning.qn.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.qingning.qn.R;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    String appId;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appId = getString(R.string.wxAppid);
        api = WXAPIFactory.createWXAPI(this, appId, false);
        api.registerApp(appId);
        api.handleIntent(getIntent(), this);
    }


    @Override
    public void onReq(BaseReq req) {
        //微信主动推送过来的消息
    }

    @Override
    public void onResp(BaseResp resp) {
        //请求响应结果
        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            // 微信登录响应事件
//            Message msg = new Message();
//            msg.obj = resp;
//            MainConstants.wxHandler.sendMessage(msg);
//            finish();
        } else if (resp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
            // 微信分享响应事件
            String result = "";
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    result = "分享成功";
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    result = "取消分享";
                    break;
                default:
                    result = "分享失败";
                    break;
            }
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        }
    }
}
